package scenic.study.thinkinjava.unit11;//: c12:ShowMethods.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Using reflection to show all the methods of 
// a class, even if the methods are defined in 
// the base class.

import java.lang.reflect.*;

public class ShowMethods {
    static final String usage =
            "usage: \n" +
                    "ShowMethods qualified.class.name\n" +
                    "To show all methods in class or: \n" +
                    "ShowMethods qualified.class.name word\n" +
                    "To search for methods involving 'word'";

    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println(usage);
            System.exit(0);
        }
        try {
            Class c = Class.forName(args[0]);
            Method[] m = c.getMethods();
            Constructor[] constructor = c.getConstructors();
            if (args.length == 1) {
                for (int i = 0; i < m.length; i++)
                    System.out.println(m[i]);
                for (int i = 0; i < constructor.length; i++)
                    System.out.println(constructor[i]);
            } else {
                for (int i = 0; i < m.length; i++)
                    if (m[i].toString()
                            .indexOf(args[1]) != -1)
                        System.out.println(m[i]);
                for (int i = 0; i < constructor.length; i++)
                    if (constructor[i].toString()
                            .indexOf(args[1]) != -1)
                        System.out.println(constructor[i]);
            }
        } catch (ClassNotFoundException e) {
            System.out.println("No such class: " + e);
        }
    }
} ///:~